package com.item.utils;

import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

@Component
public class EmailUtils {

    @Resource
    private JavaMailSender javaMailSender;

    public StringBuilder getEmail(String emailId, String email, String msg) {
        System.out.println("emailId:" + emailId);
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(emailId);
        message.setTo(email);
        message.setSubject(msg);
        Random random = new Random();
        StringBuilder code = new StringBuilder();
        for (int i = 0; i < 6; i++) {
            int r = random.nextInt(10);
            code.append(r);
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String time = sdf.format(new Date());
        System.out.println("验证码：" + code);
        String text = "您的验证码为：" + code + ",请勿泄露给他人,验证码3分钟后无效！" + time;
        message.setText(text);
        javaMailSender.send(message);
        return code;
    }
}
